package com.xxh.book.filter;

import com.xxh.book.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter(filterName = "LoginFilter",urlPatterns = {"/","/index.jsp"})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpRequest=(HttpServletRequest)req;
        HttpServletResponse httpServletResponse=(HttpServletResponse)resp;

        HttpSession session = httpRequest.getSession();

        Admin admin = (Admin)session.getAttribute("user");
        if(admin==null){
            httpServletResponse.sendRedirect(httpRequest.getContextPath()+"/login.jsp");
        }else {

        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
